{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Duck:\n",
    "    def __init__(self,input_name) :\n",
    "        self.hidden_name=input_name\n",
    "    def get_name(self):\n",
    "        print('inside the getter')\n",
    "        return self.hidden_name\n",
    "    def set_name(self,input_name):\n",
    "        print('inside the setter')\n",
    "        self.hidden_name=input_name\n",
    "    name=property(get_name,set_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "inside the getter\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'howard'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fowl=Duck('howard')\n",
    "fowl.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "inside the getter\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'howard'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fowl.get_name()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "inside the setter\n",
      "inside the setter\n"
     ]
    }
   ],
   "source": [
    "fowl.set_name('Daffy')\n",
    "fowl.name='Daffy'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Circle():\n",
    "    def __init__(self,radius): \n",
    "        self.radius=radius\n",
    "    @property\n",
    "    def diameter(self):\n",
    "        return 2* self.radius"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c=Circle(5)\n",
    "c.radius"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c.diameter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "14"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c.radius=7\n",
    "c.diameter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Duck:\n",
    "    def __init__(self,input_name) :\n",
    "        self.__name=input_name\n",
    "    def get_name(self):\n",
    "        print('inside the getter')\n",
    "        return self.__name\n",
    "    def set_name(self,input_name):\n",
    "        print('inside the setter')\n",
    "        self.__name=input_name\n",
    "    name=property(get_name,set_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "inside the getter\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'howard'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fowl=Duck('howard')\n",
    "fowl.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'howard'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 魔法访问\n",
    "fowl._Duck__name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 魔术方法 __str__()  __repr__()\n",
    "class Word():\n",
    "    def __init__(self,text) :\n",
    "        self.text=text\n",
    "    def __eq__(self, value: object) :\n",
    "        return self.text.lower()==value.text.lower()\n",
    "        \n",
    "first=Word('ha')\n",
    "second=Word('HA')\n",
    "third=Word('eh')\n",
    "first==second\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Duck(bill='wide orange', tail='long')"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from collections import namedtuple\n",
    "Duck=namedtuple('Duck','bill tail')\n",
    "duck=Duck('wide orange','long')\n",
    "duck"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wide orange\n",
      "long\n"
     ]
    }
   ],
   "source": [
    "print(duck.bill)\n",
    "print(duck.tail)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Duck(bill='wide orange', tail='long')"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "parts={'bill':'wide orange','tail':'long'}\n",
    "duck2=Duck(**parts)\n",
    "duck2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
